package cn.HuanG.login.controller;

import cn.HuanG.login.domain.LoginInfo;
import cn.HuanG.login.service.IEmpLoginService;
import cn.HuanG.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping
public class EmpLoginController {

    @Autowired
    private IEmpLoginService empLoginService;

    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/emplogin")
    public AjaxResult login(@RequestBody LoginInfo loginInfo){
        try{
            //获取前台数据
            LoginInfo emplogin =  empLoginService.countByUsername(loginInfo);
            //生成票据token
            String token = UUID.randomUUID().toString();
            //把当前用户和token 放到redis里面  并设置有效期是30分钟
            redisTemplate.opsForValue().set(token,emplogin,30, TimeUnit.MINUTES);
            Map<String, Object> map = new HashMap();
            map.put("TOKEN",token);
            map.put("emplogin",emplogin);
            System.out.println(map);
            return AjaxResult.me().setResultObj(map);
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage());
        }
    }
}
